package com.pipi.biyesheji.service.impl;

import com.google.common.collect.Lists;
import com.pipi.biyesheji.dao.manager.ProductManager;
import com.pipi.biyesheji.dao.model.ProductDO;
import com.pipi.biyesheji.dao.query.ProductQuery;
import com.pipi.biyesheji.dao.support.PageResult;
import com.pipi.biyesheji.enums.AllEnums;
import com.pipi.biyesheji.enums.BooleanEnum;
import com.pipi.biyesheji.pojo.*;
import com.pipi.biyesheji.service.CategoryService;
import com.pipi.biyesheji.service.ProductService;
import com.pipi.biyesheji.service.RoleService;
import com.pipi.biyesheji.service.UserService;
import com.pipi.biyesheji.util.BeanUtils;
import com.pipi.biyesheji.util.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductManager productManager;
    @Autowired
    private UserInfo userInfo;
    @Autowired
    private RoleService roleService;

    @Override
    public PageResult<Product> list(Page page) {
        User currentUser = userInfo.getCurrentUser();
        Role role = roleService.getRoleByUserId(currentUser.getId());
        //获得商品列表
        ProductQuery productQuery = new ProductQuery();
        productQuery.setPageNo(page.getStart());
        productQuery.setPageSize(page.getCount());
        // 如果是商家，只查询当前商家的商品
        if (AllEnums.RoleEnum.BUSINESS.getName().equals(role.getName())) {
            productQuery.createCriteria().andBidEqualTo(currentUser.getId());
        }
        PageResult<ProductDO> pageResult = productManager.selectByQueryWithPage(productQuery);
        if (CollectionUtils.isEmpty(pageResult.getResult())) {
            return PageResult.createPageResult();
        }
        List<Product> productList = pageResult.getResult().stream().map(productDO -> {
            Product product = BeanUtils.createCloneBean(productDO, Product.class);
            Category category = categoryService.get(product.getCid());
            product.setCategory(category);
            User user = userService.get(product.getBid());
            product.setUser(user);
            return product;
        }).collect(Collectors.toList());
        return PageResult.createPageResult(productList, pageResult.getPageInfo());
    }

    @Override
    public String updateStatus(String name, Integer status) {
        ProductQuery query = new ProductQuery();
        query.createCriteria().andNameEqualTo(name);

        ProductDO productDO = new ProductDO();
        productDO.setStatus(status);
        productManager.updateByQuerySelective(productDO, query);
        return "success";
    }


    @Override
    public void save(Product product) {
        ProductDO productDO = BeanUtils.createCloneBean(product, ProductDO.class);
        productDO.setStatus(BooleanEnum.YES.getCode());
        productManager.insertSelective(productDO);
        product.setId(productDO.getId());
    }

    @Override
    public void setImageURL(ProductVO vo) {
        productManager.setImageUrl(vo);
    }

    @Override
    public Product get(Long id) {
        ProductDO productDO = productManager.selectByPrimaryKey(id);
        return buildProduct(productDO);
    }

    @Override
    public void del(Long id) {
        productManager.deleteByPrimaryKey(id);
    }

    @Override
    public Category getCategoryByCid(Long id) {
        return categoryService.get(id);
    }

    @Override
    public void update(Product product) {
        ProductDO productDO = BeanUtils.createCloneBean(product, ProductDO.class);
        productManager.updateByPrimaryKeySelective(productDO);
    }

    @Override
    public List<Product> getProductsByCid(Long id) {
        ProductQuery productQuery = new ProductQuery();
        productQuery.createCriteria().andCidEqualTo(id).andStatusEqualTo(BooleanEnum.YES.getCode());
        List<ProductDO> productDOS = productManager.selectByQuery(productQuery);
        return buildProducts(productDOS);
    }

    @Override
    public User getUserByBid(Long id) {
        return userService.get(id);
    }

    @Override
    public List<Product> findByName(String pName) {
        ProductQuery productQuery = new ProductQuery();
        productQuery.createCriteria().andNameLike("%" + pName + "%");
        List<ProductDO> productDOS = productManager.selectByQuery(productQuery);
        return buildProducts(productDOS);
    }

    @Override
    public List<Product> findByCid(Long cid) {
        ProductQuery productQuery = new ProductQuery();
        productQuery.createCriteria().andCidEqualTo(cid);
        List<ProductDO> productDOS = productManager.selectByQuery(productQuery);
        return buildProducts(productDOS);
    }

    @Override
    public List<Product> getProductByBid(Long bid) {
        ProductQuery productQuery = new ProductQuery();
        productQuery.createCriteria().andBidEqualTo(bid);
        List<ProductDO> productDOS = productManager.selectByQuery(productQuery);
        return buildProducts(productDOS);
    }


    private List<Product> buildProducts(List<ProductDO> productDOS) {
        return productDOS.stream().map(this::buildProduct).collect(Collectors.toList());
    }

    private Product buildProduct(ProductDO productDO) {
        if (Objects.isNull(productDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(productDO, Product.class);
    }


}
